package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.api.mapper.WdbjMapper;
import cn.e4j.modular.crm.entity.MmrtT;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 我的备件
 */
@Service
public class WdbjService{
    @Autowired
    private WdbjMapper wdbjMapper;

    /**
     * 获取配件列表
     *
     * @param inDto
     * @return
     */
    public List<Dto> getPartListData(Page<Object>page,Dto inDto) {
        return wdbjMapper.getPartListPage(page,inDto);
    }

    /**
     * 获取维修单号和服务客户
     *
     * @param inDto
     * @return
     */
    public List<Dto> getMmrdData(Page<Object>page,Dto inDto) {
        return wdbjMapper.getMmrdPage(page,inDto);
    }

    /**
     * 配件申请提交
     *
     * @param inDto
     */
    @Transactional
    public void savePjsq(SysUser currentUser,Dto inDto) {
        Date date = new Date();
        JSONArray listArray = JSONArray.parseArray(inDto.getString("list"));
        List<MmrtT> list = listArray.toJavaList(MmrtT.class);
        String mmrfdocno = "PJSQ" + DateUtils.getDate("yyyyMMddHHmmssSSS");

        for (int i = 0;i<list.size();i++){
            JSONObject jsonObj = listArray.getJSONObject(i);
            MmrtT mmrtT = list.get(i);
            mmrtT.setMMRTENT(currentUser.getDBC());
            mmrtT.setMMRTDOCNO(mmrfdocno);
            mmrtT.setMMRTSEQ(BigDecimal.valueOf(i + 1));
            //mmrtT.setMMRTDOCDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
            mmrtT.setMMRTDOCDATE(LocalDate.now());
            mmrtT.setMMRT008(currentUser.getID());
            mmrtT.setMMRT014("");
            mmrtT.setMMRTACTI("Y");
            mmrtT.setMMRTUSER(currentUser.getID());
            mmrtT.setMMRTGRUP(currentUser.getORG_ID());
            mmrtT.setMMRTORIU(currentUser.getID());
            mmrtT.setMMRTORIG(currentUser.getORG_ID());
            //mmrtT.setMMRTDATE(DateUtils.parseDate(DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd)));
            mmrtT.setMMRTDATE(LocalDate.now());
            mmrtT.setMMRTTIME(DateUtils.formatDate(date, DateUtils.PATTERN_HH_mm_ss));

            mmrtT.setMMRT001(jsonObj.getString("MMRS001"));
            mmrtT.setMMRT002(jsonObj.getString("MMRS002"));
            mmrtT.setMMRT003(jsonObj.getString("MMRS004"));
            mmrtT.setMMRT004(jsonObj.getString("MMRS005"));
            mmrtT.setMMRT005(jsonObj.getString("MMRS006"));

            mmrtT.insert();
        }
    }
}
